package indi.zhifa.recipe.bailan.busy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.zhifa.recipe.bailan.busy.dao.dbservice.IOrmTestMybatisPlusDbService;
import indi.zhifa.recipe.bailan.busy.entity.po.OrmTestEntity;
import indi.zhifa.recipe.bailan.busy.entity.vo.PageData;
import indi.zhifa.recipe.bailan.busy.enums.EOrmEntityStatus;
import indi.zhifa.recipe.bailan.busy.service.IOrmMybatisPlusService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class OrmMybatisPlusServiceImpl extends BaseOrmServiceImpl implements IOrmMybatisPlusService {

    private final IOrmTestMybatisPlusDbService mOrmTestMybatisPlusDbService;

    @Override
    public void initTestData() {
        List<OrmTestEntity> initOrmTestEntityList = getInitOrmTestEntityList("MyBatis-Plus");
        mOrmTestMybatisPlusDbService.saveBatch(initOrmTestEntityList);
    }

    @Override
    public Page<OrmTestEntity> page(int pCurrent, int pSize,
                                        String pAppCode, String pModuleCode, String pCode, String pName, EOrmEntityStatus pStatus,
                                        Integer pMin, Integer pMax) {
        Page<OrmTestEntity> pageCfg = new Page<>(pCurrent,pSize);
        LambdaQueryWrapper<OrmTestEntity> queryWrapper = Wrappers.<OrmTestEntity>lambdaQuery();
        if(!StringUtils.isEmpty(pAppCode)){
            queryWrapper = queryWrapper.eq(OrmTestEntity::getAppCode,pAppCode);
        }
        if(!StringUtils.isEmpty(pModuleCode)){
            queryWrapper = queryWrapper.eq(OrmTestEntity::getModuleCode,pModuleCode);
        }
        if(!StringUtils.isEmpty(pCode)){
            queryWrapper = queryWrapper.eq(OrmTestEntity::getCode,pCode);
        }
        if(!StringUtils.isEmpty(pName)){
            queryWrapper = queryWrapper.like(OrmTestEntity::getName,pName);
        }
        if(null != pStatus){
            queryWrapper = queryWrapper.eq(OrmTestEntity::getStatus,pStatus);
        }
        if(null!= pMin && null != pMax){
            queryWrapper = queryWrapper.between(OrmTestEntity::getValue,pMin,pMax);
        }else if(null == pMin && null != pMax){
            queryWrapper = queryWrapper.ge(OrmTestEntity::getValue,pMax);
        }else if(null != pMax && null == pMax){
            queryWrapper = queryWrapper.le(OrmTestEntity::getValue,pMin);
        }

        Page<OrmTestEntity> pageData = mOrmTestMybatisPlusDbService.page(pageCfg,queryWrapper);

        return pageData;
    }
}
